package net.kazed.nextaction.task;

import java.text.SimpleDateFormat;
import java.util.Date;

import net.kazed.nextaction.ApplicationResource;
import net.kazed.nextaction.R;
import net.kazed.nextaction.database.Task;
import net.kazed.nextaction.database.TaskStatus;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TaskView extends LinearLayout {

    private static final String tag = "TaskView";

    protected TextView mDescription;
    protected ImageView mIcon;
    protected TextView type;
    protected TextView projectIndicator;
    protected TextView mDueDate;
    private MotionEvent downStart;
    private ApplicationResource applicationResource;

    public TaskView(Context androidContext, ApplicationResource applicationResource) {
        super(androidContext);
        this.applicationResource = applicationResource;
        
        LayoutInflater vi = (LayoutInflater) androidContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        vi.inflate(R.layout.task_list_item_view, this, true);

        mDescription = (TextView) findViewById(R.id.description);
        mIcon = (ImageView) findViewById(R.id.icon);
        type = (TextView) findViewById(R.id.type);
        projectIndicator = (TextView) findViewById(R.id.projectIndicator);
        mDueDate = (TextView) findViewById(R.id.due_date);
    }

    public void updateView(Task task, boolean isSelected) {
        // org.dodgybits.android.shuffle.model.Context context = task.context;
        // if (mShowContext && context != null) {
        // boolean displayIcon = Preferences.displayContextIcon(getContext());
        // // add context icon if preferences indicate to
        // Integer iconResource = context.iconResource;
        // if (iconResource != null && displayIcon) {
        // mIcon.setImageResource(iconResource);
        // mIcon.setVisibility(View.VISIBLE);
        // } else {
        // mIcon.setVisibility(View.GONE);
        // }
        // boolean displayContext = Preferences.displayContextName(getContext());
        // if (displayContext) {
        // mContext.setText(context.name);
        // mContext.setColourIndex(context.colourIndex, isSelected);
        // mContext.setVisibility(View.VISIBLE);
        // } else {
        // mContext.setVisibility(View.GONE);
        // }
        // } else {
        // mContext.setVisibility(View.GONE);
        // mIcon.setVisibility(View.GONE);
        // }
        CharSequence description = task.getDescription();
        if (task.getComplete()) {
            // add strike-through for completed tasks
            SpannableString desc = new SpannableString(description);
            desc.setSpan(new StrikethroughSpan(), 0, description.length(), Spanned.SPAN_PARAGRAPH);
            description = desc;
            mIcon.setVisibility(View.VISIBLE);
        } else {
            mIcon.setVisibility(View.INVISIBLE);
        }
        type.setText(getResources().getString(applicationResource.getTaskTypeAbbreviation(task.getStatus(TaskStatus.UNPROCESSED))));
        type.setBackgroundResource(applicationResource.getTaskTypeBackground(task.getStatus(TaskStatus.UNPROCESSED)));
        if (task.getNumberOfSubTasks() == null || task.getNumberOfSubTasks() == 0) {
            projectIndicator.setVisibility(View.INVISIBLE);
        } else {
            projectIndicator.setVisibility(View.VISIBLE);
        }
        mDescription.setText(description);
        if (task.getDueDate() != null) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            mDueDate.setText(dateFormat.format(task.getDueDate()));
            Date date = new Date();
            if (date.before(task.getDueDate())) {
                // task is overdue
                mDueDate.setTypeface(Typeface.DEFAULT_BOLD);
                mDueDate.setTextColor(Color.RED);
            } else {
                mDueDate.setTypeface(Typeface.DEFAULT);
                mDueDate.setTextColor(getContext().getResources().getColor(R.drawable.date_blue));
            }
            mDueDate.setVisibility(View.VISIBLE);
        } else {
            mDueDate.setVisibility(View.GONE);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // keep track of the starting down-event
            downStart = MotionEvent.obtain(event);
            Log.d(tag, "ACTION_DOWN: " + event);
            break;
        case MotionEvent.ACTION_MOVE:
            Log.d(tag, "ACTION_MOVE: " + event);
            // if moved horizontally more than slop*2, capture the event for ourselves
            float deltaX = event.getX() - downStart.getX();
            if (Math.abs(deltaX) > 10 * 2) {
                // check if we crossed an item
                float targetWidth = this.getWidth() / 4;
                float deltaY = event.getY() - downStart.getY();
                boolean movedAcross = (Math.abs(deltaX) > targetWidth);
                boolean steadyHand = (Math.abs(deltaX / deltaY) > 2);
                if (movedAcross && steadyHand) {
                    boolean crossed = (deltaX > 0);
                    if (crossed) {
                        Log.d(tag, "crossed");
                        // and return true to consume this event
                        return true;
                    }
                }
            }
            break;
        }

        return false;
    }

}
